<?php

namespace App\Http\Controllers;

use App\Models\Subscribe;
use App\Models\WechatUser;
use Illuminate\Http\Request;

class SubscribeController extends Controller
{
    public function subList()
    {
        $pn = request()->input('pn', config('api.page_num'));
        $userId = request()->input('user_id');
        $list = Subscribe::where('user_a', $userId)
            ->select('id', 'user_a', 'user_b')
            ->with('userb:id,name,head_img,point,score,score_flag')
            ->paginate($pn);
        return response()->json(['recode' => 1,
            'data' => ['last_page' => $list->lastPage()
                , 'page' => $list->currentPage()
                , 'list' => $list->items()]]);

    }

    public function fanList()
    {
        $pn = request()->input('pn', config('api.page_num'));
        $userId = request()->input('user_id');
        $list = Subscribe::where('user_b', $userId)
            ->select('id', 'user_a', 'user_b')
            ->with('usera:id,name,head_img,point,score,score_flag')
            ->paginate($pn);
        return response()->json(['recode' => 1,
            'data' => ['last_page' => $list->lastPage()
                , 'page' => $list->currentPage()
                , 'list' => $list->items()]]);

    }

    public function sub()
    {
        $userA = request()->input('usera');
        $userB = request()->input('userb');
        //检查用户
        $userAInfo = WechatUser::find($userA);
        if (!$userAInfo) {
            return response()->json(['recode' => 0, 'msg' => "用户不存在"]);
        }
        $userBInfo = WechatUser::find($userB);
        if (!$userBInfo) {
            return response()->json(['recode' => 0, 'msg' => "用户不存在"]);
        }
        //新增关注
        $sub = Subscribe::firstOrCreate(['user_a' => $userA, 'user_b' => $userB]);

        if ($sub) {
            return response()->json(['recode' => 1, 'msg' => "关注成功"]);
        }
    }

    public function can()
    {
        $userA = request()->input('usera');
        $userB = request()->input('userb');

        //取消关注
        Subscribe::where([['user_a', $userA], ['user_b', $userB]])->delete();

        return response()->json(['recode' => 1, 'msg' => "取关成功"]);
    }
}
